package org.grill.clovercash;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.grill.clovercash.accounts.Account;
import org.joda.time.LocalDate;

public class ReconciliationEditDialog extends Dialog {

	private boolean showAbort;


	public LocalDate getDate() {
		return date;
	}
	
	public void setShowAbort() {
		showAbort = true;
	}

	public void setDate(LocalDate date) {
		this.date = date;
	}

	public BigDecimal getEndAmount() {
		return endAmount;
	}

	public void setEndAmount(BigDecimal endAmount) {
		this.endAmount = endAmount;
	}

	public boolean isRecurse() {
		return recurse;
	}

	public void setRecurse(boolean recurse) {
		this.recurse = recurse;
	}

	public Account getAccount() {
		return account;
	}

	protected ReconciliationEditDialog(Shell parentShell) {
		super(parentShell);
	}
	
	private Account account;
	
	
	public void setAccount(Account account) {
		this.account = account;
	}
	
	private LocalDate date = new LocalDate();
	private boolean recurse = true;
	private BigDecimal endAmount; 
	
	private Text dateText;
	private Button recurseButton;
	private Text amountText;
	
	private static NumberFormat numberFormat = NumberFormat.getInstance();
	static {
		numberFormat.setMinimumFractionDigits(2);
		numberFormat.setMaximumFractionDigits(2);
	}
	
	protected Control createDialogArea(Composite parent) {
        Composite container = (Composite) super.createDialogArea(parent);
        GridLayout layout = (GridLayout) container.getLayout();
        layout.numColumns = 2;
        
        new Label(container, SWT.LEFT).setText("Reconciling: ");
        Label accountLabel = new Label(container, SWT.LEFT);
        accountLabel.setText(account.getDisplayName());
        accountLabel.setFont(JFaceResources.getBannerFont());
        
        new Label(container, SWT.LEFT).setText("Date: ");
        dateText = new Text(container, SWT.BORDER);
        dateText.setText(date.toString("MM-dd-YYYY"));
        dateText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        
        new Label(container, SWT.LEFT).setText("Start Amount: ");
        final Label startLabel = new Label(container, SWT.LEFT);
        startLabel.setText(NumberFormat.getCurrencyInstance().format(account.getReconciledTotal(recurse)));
        startLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        

        new Label(container, SWT.LEFT).setText("End Amount: ");
        amountText = new Text(container, SWT.BORDER);
        amountText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        if (endAmount != null)
        	amountText.setText(numberFormat.format(endAmount));
        
        recurseButton = new Button(container, SWT.CHECK);
        GridData gridData = new GridData();
        gridData.horizontalSpan = 2;
		recurseButton.setLayoutData(gridData);

		recurseButton.setSelection(recurse);
		recurseButton.addListener(SWT.MouseUp, new Listener() {
			public void handleEvent(Event arg0) {
				startLabel.setText(NumberFormat.getCurrencyInstance().format(account.getReconciledTotal(recurseButton.getSelection())));
			}
		});
		recurseButton.setText("Include Subaccounts");
        
        return container;
	}
	
	@Override
	protected void okPressed() {
		try {
			endAmount = new BigDecimal(amountText.getText());
			recurse = recurseButton.getSelection();
			date = new LocalDate(new SimpleDateFormat("MM-dd-yyyy").parse(dateText.getText()));
		} catch (Exception e) {
			e.printStackTrace();
			MessageDialog.openError(getShell(), "Input Error", "Error validating input - please ensure date and amount are correct.");
			
			return;
		}
		
		super.okPressed();
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		if (showAbort)
			createButton(parent, IDialogConstants.ABORT_ID, IDialogConstants.ABORT_LABEL, false);
        createButton(parent, IDialogConstants.OK_ID,
                IDialogConstants.OK_LABEL, true);
            createButton(parent, IDialogConstants.CANCEL_ID,
                IDialogConstants.CANCEL_LABEL, false);
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.ABORT_ID)
		{
			setReturnCode(IDialogConstants.ABORT_ID);
			close();
			return;
		}
		super.buttonPressed(buttonId);
	}

}
